import {fileURLToPath, URL} from 'node:url';

import {defineConfig, searchForWorkspaceRoot} from 'vite';
import path from 'path';
import vue from '@vitejs/plugin-vue';
import copy from 'rollup-plugin-copy';
import vueDevTools from 'vite-plugin-vue-devtools';

// https://vitejs.dev/config/
export default defineConfig(({mode}) => ({
    base: './',
    server: {
        port: 9000,
        fs: {
            allow: [
                searchForWorkspaceRoot(process.cwd()),
                path.resolve(__dirname, '../tnxjs/tnxet/node_modules/@truenewx/tnxcore/src/tnxcore.css'),
            ]
        },
    },
    build: {
        outDir: 'build',
        emptyOutDir: true,  // 构建前清空输出目录
        rollupOptions: {
            plugins: [
                copy({
                    targets: [
                        {src: 'src/renderer/assets/css/', dest: 'build/renderer/assets/'},
                        {src: 'src/renderer/assets/image/', dest: 'build/renderer/assets/'},
                        {src: 'src/resources/', dest: 'build/'},
                    ],
                    hook: 'writeBundle',
                })
            ]
        }
    },
    plugins: [
        vue(),
        mode === 'development' ? vueDevTools() : null, // 只在开发模式下启用 Vue DevTools
    ].filter(Boolean), // 过滤掉 null 值,
    resolve: {
        alias: {
            '@': fileURLToPath(new URL('./src', import.meta.url))
        }
    }
}));
